package com.el.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ThreadScopeShareData {
   /* private static  int data=0;
    private static Map<Thread,Integer> threadData=new HashMap();*/
   static ThreadLocal<Integer> x=new ThreadLocal<Integer>();
    static ThreadLocal<MyThreadScopeData> myThreadLocal=new ThreadLocal<MyThreadScopeData>();
    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                public void run() {
                   int data = new Random().nextInt();
                    System.out.println(Thread.currentThread().getName() + " has put data:" + data);
                    x.set(data);
                   /* MyThreadScopeData myThreadScopeData = new MyThreadScopeData();
                    myThreadScopeData.setAge(data);
                    myThreadScopeData.setName("name:"+data);*/
                    MyThreadScopeData instance = MyThreadScopeData.getInstance();
                    instance.setName("name:"+data);
                    instance.setAge(data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }
    static class A{
      public  void get(){
          int data=x.get();
          System.out.println("A fromm "+Thread.currentThread().getName()+" get data:" +data);
          MyThreadScopeData myThreadScopeData = myThreadLocal.get();
         /* myThreadScopeData.getAge();
          myThreadScopeData.getName();
          System.out.println("A from "+Thread.currentThread().getName()+
          "getMydata:"+myThreadScopeData.getName()+",,,"+myThreadScopeData.getAge());*/
          MyThreadScopeData instance = MyThreadScopeData.getInstance();
          System.out.println("A from "+Thread.currentThread().getName()+
                  "getMydata:"+instance.getName()+",,,"+instance.getAge());
      }
    }
    static class B{
        public  void get(){
            int data=x.get();
            System.out.println("B fromm "+Thread.currentThread().getName()+" get data:" +data);
            MyThreadScopeData instance = MyThreadScopeData.getInstance();
            System.out.println("B from "+Thread.currentThread().getName()+
                    "getMydata:"+instance.getName()+",,,"+instance.getAge());

        }
    }
}
class MyThreadScopeData{
    private MyThreadScopeData(){

    }
    public static synchronized MyThreadScopeData getInstance(){
        if(instance==null){
            instance=new MyThreadScopeData();
            map.set(instance);
        }
        return instance;
    }
    //饱汗
  //  private static MyThreadScopeData instance=new MyThreadScopeData();
    private static MyThreadScopeData instance=null;
    private static ThreadLocal<MyThreadScopeData> map=new ThreadLocal<MyThreadScopeData>();
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
